package com.shuyigou.service;

import com.shuyigou.mapper.OrderMapper;
import com.shuyigou.pojo.Order;
import com.shuyigou.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class OrderService {


    public List<Order> selectByCustomerId(int customer_id) {
            //1、获取sqlSessionFactory对象
            SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
            //2、获取sqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //3、获取Mapper
            OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
           //4、调用方法
           List<Order> orders = mapper.selectByCustomerId(customer_id);
           //5、释放资源
           sqlSession.close();
           return orders;
    }


    public void addOrder(String ISBN, int customer_id, int quantity) {
        //1、获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //2、获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3、获取Mapper
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        //4、调用方法
        mapper.addOrder(ISBN, customer_id, quantity);
        sqlSession.commit();
        sqlSession.close();
    }

}
